package com.gmu.controller;


import com.alibaba.excel.EasyExcel;
import com.gmu.domain.ResResult;
import com.gmu.domain.vo.PageVo;
import com.gmu.mapper.TblActivitiesMapper;
import com.gmu.pojo.Page;
import com.gmu.pojo.TblActivities;
import com.gmu.service.TblActivitiesService;
import com.gmu.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 测核酸活动 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Api(value = "活动管理", tags= "活动管理API")
@RestController
@RequestMapping("/gmu/tbl-activities")
public class TblActivitiesController {

    @Autowired
    private TblActivitiesService tblActivitiesService;

    @Autowired
    private TblActivitiesMapper tblActivitiesMapper;

    /**
     * 提交一次活动
     * @param tblActivities
     * @return
     */
    @PreAuthorize("hasAuthority('sys:activitymanagegover')")
    @ApiOperation(value = "提交一次活动")
    @PostMapping("/commitActivities")
    public ResResult<TblActivities> commitActivities(@RequestBody TblActivities tblActivities) {
        return tblActivitiesService.commitActivities(tblActivities);
    }

    @ApiOperation(value = "展示可预约活动列表")
    @GetMapping("/showReservationActivities/{unitsId}")
    public ResResult<?> showReservationActivities(@PathVariable Integer unitsId) {
        return tblActivitiesService.showReservationActivities(unitsId);
    }

    /**
     * 获取所有活动
     * @return
     */
    @PreAuthorize("hasAnyAuthority('sys:activitymanageschool','sys:activitymanagegover')")
    @ApiOperation(value = "获取所有活动(分页)")
    @PostMapping("/getAllActivitiesByPage")
    public ResResult<?> getAllActivitiesByPage(@RequestBody Page page){
        List<TblActivities> tblActivities = tblActivitiesMapper.getAllActivitiesByPage(PageUtil.getPageMap(page));
        Integer total = tblActivitiesMapper.selectCount(null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("tblActivities",tblActivities);
        return new ResResult<>(200,map);
    }

    /**
     * 获取所有未举行活动
     */
    @ApiOperation(value = "获取所有未举行活动")
    @GetMapping("/getAllActivitiesByStatus/{beSampleUnitsId}")
    public ResResult<?> getAllActivitiesByStatus(@PathVariable Integer beSampleUnitsId){
        List<TblActivities> tblActivities = tblActivitiesMapper.getAllActivitiesByStatus(beSampleUnitsId);
        return new ResResult<>(200,tblActivities);
    }

    /**
     * 获取所有已举行及结束的活动
     */
    @ApiOperation(value = "获取所有举行中及结束的活动")
    @GetMapping("/getAllActivitiesNotByStatus/{beSampleUnitsId}")
    public ResResult<?> getAllActivitiesNotByStatus(@PathVariable Integer beSampleUnitsId){
        List<TblActivities> tblActivities = tblActivitiesMapper.getAllActivitiesNotByStatus(beSampleUnitsId);
        return new ResResult<>(200,tblActivities);
    }

    /**
     * 通过unitsId获取已举行及结束的活动
     */
    @ApiOperation("通过unitsId获取已举行及结束的活动")
    @GetMapping("/getAllActivitiesByUnitsId/{unitsId}")
    public ResResult<?> getAllActivitiesByUnitsId(@PathVariable Integer unitsId){
        return tblActivitiesService.getAllActivitiesByUnitsId(unitsId);
    }

    /**
     * 通过unitsId获取举行中的活动
     */
    @ApiOperation("通过unitsId获取举行中的活动")
    @GetMapping("/getAllActivitiesHeld/{unitsId}")
    public ResResult<?> getAllActivitiesHeld(@PathVariable Integer unitsId){
        return tblActivitiesService.getAllActivitiesHeld(unitsId);
    }

    /**
     * 删除活动
     */
    @PreAuthorize("hasAuthority('sys:activitymanagegover')")
    @ApiOperation(value = "删除活动")
    @DeleteMapping("/deleteActivities/{activitiesId}")
    public ResResult<?> deleteActivities(@PathVariable Integer activitiesId){
        if ((tblActivitiesMapper.deleteById(activitiesId)) < 1){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"删除成功");
    }

    /**
     * 修改活动
     */
    @PreAuthorize("hasAuthority('sys:activitymanagegover')")
    @ApiOperation(value = "修改活动")
    @PutMapping("updateActivities")
    public ResResult<?> updateActivities(@RequestBody TblActivities tblActivities){
        if ((tblActivitiesMapper.updateById(tblActivities)) < 1){
            return new ResResult<>(500,"修改失败");
        }
        return new ResResult<>(200,"修改成功");
    }

    @ApiOperation(value = "pc端核酸检测活动记录")
    @GetMapping("/activityRecordList")
    public PageVo<?> getActivityRecordList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, TblActivities tblActivities, String beginTime, String endTime) {
        return tblActivitiesService.getActivityRecordList(pageNum,pageSize,tblActivities,beginTime,endTime);
    }

    @ApiOperation(value = "pc端批量删除活动记录")
    @DeleteMapping("/deleteRecord/{activitiesIds}")
    public ResResult<?> deleteRecord(@PathVariable Integer[] activitiesIds) {
        return tblActivitiesService.deleteRecord(activitiesIds);
    }

    @ApiOperation(value = "导出活动记录数据")
    @GetMapping("/exportRecord")
    public void exportRecord(HttpServletResponse response,TblActivities tblActivities, String beginTime, String endTime) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("核酸检测活动记录","UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Access-Control-Expose-Headers","Content-disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            List<TblActivities> list = tblActivitiesService.exportRecord(tblActivities,beginTime,endTime);
            EasyExcel.write(response.getOutputStream(),TblActivities.class).sheet("核酸检测活动记录").doWrite(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "pc端查询获取某个核算检测活动信息")
    @GetMapping("/{activitiesId}")
    public ResResult<?> getTblActivitiesOne(@PathVariable Integer activitiesId) {
        TblActivities tblActivities = tblActivitiesService.getById(activitiesId);
        return new ResResult<>(200,tblActivities);
    }

}

